package com.kang.controller;

import com.kang.feign.UserOssFeign;
import com.kang.util.WebResponse;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


@RestController
@Api(tags = "测试接口")
@RequestMapping("/user/test")
@Slf4j
public class TestController {

    @Autowired
    private UserOssFeign userOssFeign;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 异步保存的过程中调用保存接口，内部调用oss上传接口
     * 1,上传到本地 返回文件名称
     * 2,点击保存后获取本地文件上传到oss中返回文件名
     * 2,定时任务清空本地目录
     */
    @PostMapping("test")
    @Async
    public String test(String fileName) throws IOException {
//        File file = new File("E:\\upAndDownload\\download" + "\\" + fileName + ".png");
        File file = new File("E:\\upAndDownload\\upload" + "\\" + fileName + ".mp4");
        System.out.println(file);
        MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
        WebResponse webResponse = userOssFeign.videoFileUpload(multipartFile);
        /**
         * 此处异步加载可以正常获取数据
         * 获取照片id再存数据库一样
         */
        return (String) webResponse.getData();
    }

    @GetMapping("test666")
    public void test666(String fileName){


        stringRedisTemplate.opsForValue().increment("test");

        String test = stringRedisTemplate.opsForValue().get("test");
        System.out.println(test);

    }

}
